package com.wan.task;

import java.util.ArrayList;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.wan.commonsdk.Conet;
import com.wan.commonsdk.MD5Utils;
import com.wan.commonsdk.PayInfo;
import com.wan.commonsdk.YHQInfo;
import com.wan.taskListener.PayOrderYHQTaskListener;

import android.os.AsyncTask;

public class PayYHQTask extends AsyncTask {
	private Map<String, String> map;
	private PayOrderYHQTaskListener yhqListener;
	ArrayList<YHQInfo> list;
	private PayInfo payInfo;
	private int page;
	public PayYHQTask(PayInfo payInfo,int page) {
		this.payInfo = payInfo;
		this.page = page;
	}
	public void setYhqListener(PayOrderYHQTaskListener yhqListener) {
		this.yhqListener = yhqListener;
	}

	@Override
	protected Object doInBackground(Object... params) {
		 list = new ArrayList<YHQInfo>();
		String time = System.currentTimeMillis() + "";
		int tim = (int)(Long.parseLong(time)/1000);
		String auth = Conet.mWanAppInfo.getPlatformgid() +"19"
				+ Conet.mUserInfo.getUser_id() + "" + tim + "#"
				+ "8#37@2454A227f##@72240@8b3Ad7367";
		String auths = MD5Utils.getMD5(auth);
		String url = Conet.YHQListUrl + "&gameid="
				+ Conet.mWanAppInfo.getPlatformgid() + "&channelid=19"
				+ "&userid=" + Conet.mUserInfo.getUser_id() + "&time=" + tim
				+ "&auth=" + auths + "&money="+payInfo.getFixedmoney() +"&page="+page;
		try {

			HttpGet httpGet = new HttpGet(url);
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(httpGet);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				String strResult = EntityUtils.toString(
						httpResponse.getEntity(), "UTF-8");
				
				JSONObject jsonObject = new JSONObject(strResult);
				String other = jsonObject.optString("other");
				String check = jsonObject.optString("check");
				JSONArray jsonArray = jsonObject.optJSONArray("data");
				for (int i = 0; i < jsonArray.length(); i++) {
					jsonObject = jsonArray.optJSONObject(i);
					// {
					// "id": "349815",
					// "gift_id": "487",
					// "lotname": "201512%E6%B5%8B%E8%AF%95%E7%A4%BC%E5%88%B85",
					// "giftcode": "53D85B2A6E77",
					// "price": "5.00",
					// "min_price": "6.00",
					// "active_time": "2015-11-24",
					// "game": "",
					// "off_data": "2015-12-13",
					// "status": 1
					// },
					YHQInfo yhqInfo = new YHQInfo();
					yhqInfo.setActive_time(jsonObject.optString("active_time"));
					yhqInfo.setGame(jsonObject.optString("game"));
					yhqInfo.setGift_id(jsonObject.getString("gift_id"));
					yhqInfo.setGiftcode(jsonObject.optString("giftcode"));
					yhqInfo.setId(jsonObject.optString("id"));
					yhqInfo.setLotname(jsonObject.optString("lotname"));
					yhqInfo.setMin_orice(jsonObject.optString("min_price"));
					yhqInfo.setOff_data(jsonObject.optString("off_data"));
					yhqInfo.setPrice(jsonObject.optString("price"));
					yhqInfo.setStatus(jsonObject.optString("status"));
					yhqInfo.setOther(other);
					yhqInfo.setCheck(check);
					list.add(yhqInfo);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			return list;
		}
		return list;
	}

	@Override
	protected void onPostExecute(Object result) {
		yhqListener.yhq_result(list);
	}
}
